#include "playscene.h"

PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
{

}

PlayScene::PlayScene(int index)
{
//    QString str = QString("进入了第 %1 关 ").arg(index);
//    qDebug() << str;

    qDebug() << "当前关卡为"<< index;
    this->levalIndex = index;
    //设置窗口固定大小
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    //设置标题
    this->setWindowTitle("翻金币场景");

    //创建菜单栏
    QMenuBar * bar = this->menuBar();
    this->setMenuBar(bar);
    //创建开始菜单
    QMenu * startMenu = bar->addMenu("开始");
    //创建按钮菜单项
    QAction * quitAction = startMenu->addAction("退出");
    //点击退出 退出游戏
    connect(quitAction,&QAction::triggered,[=](){this->close();});

    // 添加音效资源
    //返回按钮音效
    QSound *backSound = new QSound(":/res/BackButtonSound.wav",this);
    // 翻金币音效
    QSound *flipSound = new QSound(":/res/ConFlipSound.wav",this);
    //胜利按钮音效
    QSound *winSound = new QSound(":/res/LevelWinSound.wav",this);


    //返回按钮
   MyPushButton * closeBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
   closeBtn->setParent(this);
   closeBtn->move(this->width()-closeBtn->width(),this->height()-closeBtn->height());

   //返回按钮功能实现
   connect(closeBtn,&MyPushButton::clicked,[=](){
       qDebug() << "翻金币场景中：点击了返回按钮";

       // 播放返回按钮音效
       backSound->play();

       QTimer::singleShot(500, this,[=](){
           this->hide();
           //触发自定义信号，关闭自身，该信号写到 signals下做声明
           emit this->chooseSceneBack();
            }
       );
   });

   //当前关卡标题
   QLabel * label = new QLabel;
   label->setParent(this);
   QFont font;
   font.setFamily("华文新魏");
   font.setPointSize(20);
    //将字体设置到标签控件中
   label->setFont(font);
   QString str = QString("Leavel: %1").arg(this->levalIndex);
   label->setText(str);
   label->setGeometry(QRect(30, this->height() - 50,150, 50)); //设置大小和位置


   //初始化二维数组
   dataConfig config;
   for(int i = 0 ; i < 4;i++)
   {
       for(int j = 0 ; j < 4; j++)
       {
           gameArray[i][j] = config.mData[this->levalIndex][i][j];
       }
   }

   // 胜利图片显示
   QLabel* winLabel = new QLabel;
   QPixmap tmpPix;
   tmpPix.load(":/res/LevelCompletedDialogBg.png");
   winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
   winLabel->setPixmap(tmpPix);
   winLabel->setParent(this);
   winLabel->move( (this->width() - tmpPix.width())*0.5 , -tmpPix.height());



   //显示金币的背景图片
   for(int i = 0 ; i < 4;i++)
   {
       for(int j = 0 ; j < 4; j++)
       {
          //绘制背景图片
           QLabel* label = new QLabel;
           label->setGeometry(0,0,50,50);
           label->setPixmap(QPixmap(":/res/BoardNode(1).png"));
           label->setParent(this);
           label->move(57 + i*50,200+j*50);

           //创建金币对象
           QString img;
           if(gameArray[i][j] == 1)
           {
               // 显示金币
             img = ":/res/Coin0001.png";
           }
           else
           {
             img = ":/res/Coin0008.png";
           }
           MyCoin * coin = new MyCoin(img);
           coin->setParent(this);
           coin->move(59 + i*50,204+j*50);


           coin->posX = i; //记录x坐标
           coin->posY = j; //记录y坐标
           coin->flag =gameArray[i][j]; //记录正反标志

           coinBtn[i][j] = coin; // 将金币放到二维数组中 方便后续的维护
           //点击金币 进行翻转
           connect(coin,&MyCoin::clicked,[=](){
               // 播放翻金币的音效
               flipSound->play();

               // 点击按钮将所有按钮都先禁用
               for(int i = 0 ; i < 4 ; i++)
               {
                   for(int j = 0 ; j < 4 ; j++)
                   {
                       this->coinBtn[i][j]->isWin = true;
                   }
               }

               //qDebug() << "点击的位置： x = " <<  coin->posX << " y = " << coin->posY ;
               coin->changeFlag();
               gameArray[i][j] = gameArray[i][j] == 0 ? 1 : 0; //数组内部记录的标志同步修改

               //延时翻动其他周围金币
               QTimer::singleShot(300, this,[=](){
                   if(coin->posX+1 <=3)// 右
                   {
                     coinBtn[coin->posX+1][coin->posY]->changeFlag();
                     gameArray[coin->posX+1][coin->posY] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;
                   }
                   if(coin->posX-1>=0)// 左
                   {
                     coinBtn[coin->posX-1][coin->posY]->changeFlag();
                     gameArray[coin->posX-1][coin->posY] = gameArray[coin->posX-1][coin->posY]== 0 ? 1 : 0;
                   }
                   if(coin->posY+1<=3)// 上
                   {
                    coinBtn[coin->posX][coin->posY+1]->changeFlag();
                    gameArray[coin->posX][coin->posY+1] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;
                   }
                   if(coin->posY-1>=0)// 下
                   {
                    coinBtn[coin->posX][coin->posY-1]->changeFlag();
                    gameArray[coin->posX][coin->posY-1] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;
                   }

                   // 翻完所有金币后 将所有金币解开禁用
                   for(int i = 0 ; i < 4 ; i++)
                   {
                       for(int j = 0 ; j < 4 ; j++)
                       {
                           this->coinBtn[i][j]->isWin = false;
                       }
                   }

                   //判断是否胜利
                   this->isWin = true;
                   for(int i = 0 ; i < 4;i++)
                   {
                       for(int j = 0 ; j < 4; j++)
                       {
                           //qDebug() << coinBtn[i][j]->flag ;
                           if( coinBtn[i][j]->flag == false)
                           {
                               this->isWin = false;
                               break;
                           }
                       }
                   }
                   //如果isWin依然是true，代表胜利了！
                   if(this->isWin)
                  {
                         qDebug() << "胜利";

                         // 播放胜利音效
                         winSound->play();

                         //禁用所有按钮点击事件  将所有按钮的胜利标志改为true
                         for(int i = 0 ; i < 4;i++)
                         {
                             for(int j = 0 ; j < 4; j++)
                             {
                               coinBtn[i][j]->isWin = true;
                             }
                         }
                         // 如果胜利了，将上面的图片移动下来
                         if(this->isWin)
                             {
                            qDebug() << "胜利";
                             QPropertyAnimation * animation1 =  new QPropertyAnimation(winLabel,"geometry");
                             // 设置时间间隔
                             animation1->setDuration(1000);
                             // 设置开始位置
                             animation1->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                             // 设置结束位置
                             animation1->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
                             // 设置缓和曲线
                             animation1->setEasingCurve(QEasingCurve::OutBounce);
                             // 执行动画
                             animation1->start();
                             }

                  }



               });


           });






       }
   }





}

// 绘画事件
void PlayScene::paintEvent(QPaintEvent *)
{
    //加载背景
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap( 10,30,pix.width(),pix.height(),pix);



}


